#ifndef SOURCE_H
#define SOURCE_H

#include <QObject>

#include "invalidprice.h"

#include <QString>
#include <QUrl>
#include <QMap>
#include <QSet>

class PriceGrabber;
class NameSpace;
class UniqueName;


class Source : public QObject
{
    Q_OBJECT

public:
    explicit Source(
        NameSpace *nameSpace
    ,   const QString &name
    ,   const int price = INVALID_PRICE
    ,   const QUrl &url = QUrl());

    virtual ~Source();

    static const QString host(const QUrl &url);


    void setName(const QString &name);
    const QString name() const;

    void setPrice(const int price);
    const int price() const;

    void setQuantity(const uint quantity);
    const uint quantity() const;

    void setUrl(const QUrl &url);
    const QUrl &url() const;

    void setIgnored(const bool ignored);
    const bool ignored() const;


    const int differenceToPreviousPrice() const;

    const bool isValid() const;
    const QString &error() const;

    const bool isUpdating() const;

signals:
    void modified();
    void ignoreStateChanged(bool ignored);

    void nameChanged(const QString &name);
    void priceChanged(int);
    void quantityChanged(uint quantity);
    void urlChanged(const QUrl &url);

    void updateStarted();
    void updated(bool succeeded);
    void canceled();

public slots:
    void update();
    void cancel();

protected slots:
    void priceGrabberSucceeded(int price);
    void priceGrabberFailed(QString error);

protected:
    bool m_ignored;

    UniqueName *m_name;

    int m_price;
    int m_previousPrice;

    uint m_quantity;
    QUrl m_url;

    QString m_error;

    PriceGrabber *m_priceGrabber;
};

#endif // SOURCE_H
